<template>
  <div>
    <el-form-item
      v-for="resource in resourceList" :key="resource.name" :label="resource.label" :prop="resource.name"
      :rules="[{required: true, trigger: ['blur'], validator: (rule, value, cb) => {
        if (!resourceForm[resource.name] || resourceForm[resource.name].length < 1) {
          return cb(error('请输入'+resource.label))
        } else {
          return cb()
        }
      }}]"
    >
      <starrocks-api-databases-select
        v-if="authService==='STARROCKS_API'&&resource.name==='DATABASE'"
        v-model="resourceForm.DATABASE"
        :add-star="true" :allow-create="allowCreate()"  :mode="mode" :placeholder="'请选择'+resource.label"
      />
      <starrocks-api-tables-select
        v-else-if="authService==='STARROCKS_API'&&resource.name==='TABLE'"
        v-model="resourceForm.TABLE"
        :add-star="true" :allow-create="allowCreate()"  :mode="mode" :databases="resourceForm.DATABASE"
        :placeholder="'请选择'+resource.label"
      />

      <starrocks-analysis-databases-select
        v-if="authService==='STARROCKS_ANALYSIS'&&resource.name==='DATABASE'"
        v-model="resourceForm.DATABASE"
        :add-star="true" :allow-create="allowCreate()"  :mode="mode" :placeholder="'请选择'+resource.label"
      />
      <starrocks-analysis-tables-select
        v-else-if="authService==='STARROCKS_ANALYSIS'&&resource.name==='TABLE'"
        v-model="resourceForm.TABLE"
        :add-star="true" :allow-create="allowCreate()"  :mode="mode" :databases="resourceForm.DATABASE"
        :placeholder="'请选择'+resource.label"
      />

      <starrocks-wodata-catalogs-select
        v-if="authService==='STARROCKS_WODATA'&&resource.name==='CATALOG'"
        v-model="resourceForm.CATALOG"
        :add-star="true" :allow-create="allowCreate()"  :mode="mode" :placeholder="'请选择'+resource.label"
      />
      <template v-if="authService==='STARROCKS_WODATA'&&resource.name==='DATABASE'">
        <starrocks-wodata-databases-select
          v-if="authType==='FUNCTION'||authType==='VIEW'"
          v-model="resourceForm.DATABASE" :catalogs="starrocksWodataDefaultCatalog"
          :add-star="true" :allow-create="allowCreate()"  :mode="mode" :placeholder="'请选择'+resource.label"
        />
        <starrocks-wodata-databases-select
          v-else
          v-model="resourceForm.DATABASE" :catalogs="resourceForm.CATALOG"
          :add-star="true" :allow-create="allowCreate()"  :mode="mode" :placeholder="'请选择'+resource.label"
        />
      </template>
      <starrocks-wodata-tables-select
        v-else-if="authService==='STARROCKS_WODATA'&&resource.name==='TABLE'"
        v-model="resourceForm.TABLE"
        :add-star="true" :allow-create="allowCreate()"  :mode="mode" :catalogs="resourceForm.CATALOG"
        :databases="resourceForm.DATABASE"
        :placeholder="'请选择'+resource.label"
      />
      <starrocks-wodata-views-select
        v-else-if="authService==='STARROCKS_WODATA'&&resource.name==='VIEW'"
        v-model="resourceForm.VIEW"
        :add-star="true" :allow-create="allowCreate()"  :mode="mode" :databases="resourceForm.DATABASE"
        :placeholder="'请选择'+resource.label"
      />
      <starrocks-wodata-global-functions-select
        v-else-if="authService==='STARROCKS_WODATA'&&resource.name==='FUNCTION'"
        v-model="resourceForm.FUNCTION"
        :add-star="true" :allow-create="allowCreate()"  :mode="mode" :databases="resourceForm.DATABASE"
        :placeholder="'请选择'+resource.label"
      />
      <starrocks-wodata-functions-select
        v-if="authService==='STARROCKS_WODATA'&&resource.name==='GLOBAL_FUNCTION'"
        v-model="resourceForm.GLOBAL_FUNCTION"
        :add-star="true" :allow-create="allowCreate()"  :mode="mode" :placeholder="'请选择'+resource.label"
      />

      <hive-databases-select
        v-else-if="authService==='HIVE'&&resource.name==='DATABASE'"
        v-model="resourceForm.DATABASE"
        :add-star="true" :allow-create="allowCreate()"  :mode="mode" :placeholder="'请选择'+resource.label"
      />
      <hive-tables-select
        v-else-if="authService==='HIVE'&&resource.name==='TABLE'"
        v-model="resourceForm.TABLE"
        :add-star="true" :allow-create="allowCreate()"  :mode="mode" :databases="resourceForm.DATABASE"
        :placeholder="'请选择'+resource.label"
      />
      <hive-columns-select
        v-else-if="authService==='HIVE'&&resource.name==='COLUMN'"
        v-model="resourceForm.COLUMN"
        :add-star="true" :allow-create="allowCreate()"  :mode="mode" :databases="resourceForm.DATABASE"
        :placeholder="'请选择'+resource.label"
        :tables="resourceForm.TABLE"
      />
      <hive-udfs-select
        v-else-if="authService==='HIVE'&&resource.name==='FUNCTION'"
        v-model="resourceForm.FUNCTION"
        :add-star="true" :allow-create="allowCreate()"  :mode="mode" :placeholder="'请选择'+resource.label"
      />

      <presto-catalogs-select
        v-else-if="authService==='PRESTO'&&resource.name==='CATALOG'"
        v-model="resourceForm.CATALOG" :add-star="true" :allow-create="allowCreate()"  :mode="mode"
        :placeholder="'请选择'+resource.label"
      />
      <presto-schems-select
        v-else-if="authService==='PRESTO'&&resource.name==='SCHEMA'"
        v-model="resourceForm.SCHEMA" :add-star="true" :allow-create="allowCreate()"  :mode="mode"
        :catalogs="resourceForm.CATALOG" :placeholder="'请选择'+resource.label"
      />
      <presto-tables-select
        v-else-if="authService==='PRESTO'&&resource.name==='TABLE'"
        v-model="resourceForm.TABLE" :add-star="true" :allow-create="allowCreate()"  :mode="mode"
        :catalogs="resourceForm.CATALOG"
        :placeholder="'请选择'+resource.label" :schems="resourceForm.SCHEMA"
      />
      <presto-columns-select
        v-else-if="authService==='PRESTO'&&resource.name==='COLUMN'"
        v-model="resourceForm.COLUMN" :add-star="true"
        :allow-create="allowCreate()"  :mode="mode" :catalogs="resourceForm.CATALOG"
        :schems="resourceForm.SCHEMA"
        :placeholder="'请选择'+resource.label" :tables="resourceForm.TABLE"
      />
      <presto-functions-select
        v-else-if="authService==='PRESTO'&&resource.name==='FUNCTION'"
        v-model="resourceForm.FUNCTION" :add-star="true"
        :allow-create="allowCreate()"  :mode="mode" :placeholder="'请选择'+resource.label"
      />
    </el-form-item>
  </div>
</template>

<script>
import PrestoFunctionsSelect from "@/views/auth/meta/presto/prestoFunctionsSelect";
import PrestoSchemsSelect from "@/views/auth/meta/presto/prestoSchemsSelect";
import PrestoTablesSelect from "@/views/auth/meta/presto/prestoTablesSelect";
import PrestoColumnsSelect from "@/views/auth/meta/presto/prestoColumnsSelect";
import PrestoCatalogsSelect from "@/views/auth/meta/presto/prestoCatalogsSelect";
import HiveColumnsSelect from "@/views/auth/meta/hive/hiveColumnsSelect";
import HiveTablesSelect from "@/views/auth/meta/hive/hiveTablesSelect";
import HiveDatabasesSelect from "@/views/auth/meta/hive/hiveDatabasesSelect";
import StarrocksApiDatabasesSelect from "@/views/auth/meta/starrocks/starrocksApiDatabasesSelect.vue";
import StarrocksApiTablesSelect from "@/views/auth/meta/starrocks/starrocksApiTablesSelect.vue";
import {listResource} from "@/api/auth/auth";
import HiveUdfsSelect from "@/views/auth/meta/hive/hiveUdfsSelect.vue";
import StarrocksAnalysisDatabasesSelect from "@/views/auth/meta/starrocks/starrocksAnalysisDatabasesSelect.vue";
import StarrocksAnalysisTablesSelect from "@/views/auth/meta/starrocks/starrocksAnalysisTablesSelect.vue";
import StarrocksWodataDatabasesSelect from "@/views/auth/meta/starrocks/starrocksWodataDatabasesSelect.vue";
import StarrocksWodataTablesSelect from "@/views/auth/meta/starrocks/starrocksWodataTablesSelect.vue";
import StarrocksWodataCatalogsSelect from "@/views/auth/meta/starrocks/starrocksWodataCatalogsSelect.vue";
import StarrocksWodataViewsSelect from "@/views/auth/meta/starrocks/starrocksWodataViewsSelect.vue";
import StarrocksWodataGlobalFunctionsSelect from "@/views/auth/meta/starrocks/starrocksWodataGlobalFunctionsSelect.vue";
import StarrocksWodataFunctionsSelect from "@/views/auth/meta/starrocks/starrocksWodataFunctionsSelect.vue";

export default {
  name: "ApplyAuthInfoResourceDiv",
  components: {
    StarrocksWodataFunctionsSelect,
    StarrocksWodataGlobalFunctionsSelect,
    StarrocksWodataViewsSelect,
    StarrocksWodataCatalogsSelect,
    StarrocksWodataTablesSelect,
    StarrocksWodataDatabasesSelect,
    StarrocksAnalysisTablesSelect,
    StarrocksAnalysisDatabasesSelect,
    HiveUdfsSelect,
    PrestoFunctionsSelect,
    PrestoSchemsSelect,
    PrestoTablesSelect,
    PrestoColumnsSelect,
    PrestoCatalogsSelect,
    HiveColumnsSelect,
    HiveTablesSelect,
    HiveDatabasesSelect,
    StarrocksApiDatabasesSelect,
    StarrocksApiTablesSelect
  },
  props: {
    value: {type: Object, default: null},
    mode: {type: String, default: 'NORMAL'},
    authService: {type: String, default: null},
    authType: {type: String, default: null},
  },
  data() {
    return {
      resourceList: [],
      resourceForm: {},
      starrocksWodataDefaultCatalog: ['default_catalog']
    }
  },
  watch: {
    'value': {
      handler(val) {
        this.resourceForm = val
      }, immediate: true
    },
    'resourceForm': {
      handler(val) {
        this.$emit('input', val)
      }, immediate: true
    },
    'authType': {
      handler() {
        if (this.authService && this.authType) {
          listResource(this.mode, this.authService, this.authType).then(res => {
            this.resourceList = res.data
          })
        } else {
          this.resourceList = []
        }
      }, immediate: true
    },
  },
  methods: {
    error(msg) {
      return new Error(msg)
    },

    allowCreate() {
      // return this.mode === 'NORMAL';
      return true
    }
  }
}
</script>

<style scoped>

</style>
